﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Drawing;
using System.Net;

namespace Twite
{
    class Storage
    {
        public List<Status> timeLine;
        private Dictionary<String, Bitmap> profileImage;

        public Storage()
        {
            timeLine = new List<Status>();
            profileImage = new Dictionary<string, Bitmap>();
        }

        public void AddTimeLine(Status item)
        {
            if (!timeLine.Contains(item))
            {
                timeLine.Add(item);
                GetImage(item.user.profileImageUrl);
            }
        }

        public Bitmap GetImage(string url)
        {
            if (profileImage.ContainsKey(url))
            {
                return (profileImage[url]);
            }
            else
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = "GET";

                WebResponse responce;
                try
                {
                    responce = req.GetResponse();
                }
                catch (WebException)
                {
                    return new Bitmap(48, 48);
                }

                return (profileImage[url] = new Bitmap(responce.GetResponseStream()));
            }
        }
    }

    public class Status : IComparable<Status>, IEquatable<Status>
    {
        public DateTime createdAt;
        public int id;
        public string text;
        public string source;
        public int inReplyToStatusId;
        public int inReplyToUserId;
        public string inReplyToScreenName;
        public bool favorited;
        public StatusUser user;
        public bool read;

        public Status(XElement status)
        {
            try
            {
                this.createdAt = DateTime.ParseExact(status.Element("created_at").Value, "ddd MMM dd HH:mm:ss zzzz yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo, System.Globalization.DateTimeStyles.None);
            }
            catch(FormatException) { }

            this.id = Convert.ToInt32(status.Element("id").Value);
            this.text = status.Element("text").Value;
            this.source = status.Element("source").Value;
            try
            {
                this.inReplyToStatusId = Convert.ToInt32(status.Element("in_reply_to_status_id").Value);
                this.inReplyToUserId = Convert.ToInt32(status.Element("in_reply_to_user_id").Value);
                this.inReplyToScreenName = status.Element("in_reply_to_screen_name").Value;
            }
            catch (FormatException) { }
            this.favorited = Convert.ToBoolean(status.Element("favorited").Value);
            this.user = new StatusUser(status.Element("user"));
            this.read = false;
        }

        public Status(int id)
        {
            this.id = id;
        }

        public override string ToString() 
        {
            return (user.screenName + ":" + text + " [http://twitter.com/" + user.screenName + "/statuses/" + id + "]");
        }

        public bool Equals(Status item)
        {
            return (this.id == item.id);
        }


        public int CompareTo(Status item)
        {
            return(this.id - item.id);
        }

        public class StatusUser
        {
            public int id;
            public string name;
            public string screenName;
            public string location;
            public string description;
            public string profileImageUrl;
            public string url;
            public bool userProtected;
            public int followersCount;

            public StatusUser(XElement user)
            {
                this.id = Convert.ToInt32(user.Element("id").Value);
                this.name = user.Element("name").Value;
                this.screenName = user.Element("screen_name").Value;
                this.location = user.Element("location").Value;
                this.description = user.Element("description").Value;
                this.profileImageUrl = user.Element("profile_image_url").Value;
                this.userProtected = Convert.ToBoolean(user.Element("protected").Value);
                this.followersCount = Convert.ToInt32(user.Element("followers_count").Value);
            }
        }

    }
}
